package com.metaera.app.ui.popup;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import com.lxj.xpopup.core.BottomPopupView;
import com.metaera.app.MetaEraApplication;
import com.metaera.app.R;
import com.metaera.app.utils.ToastUtils;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.DrawableCompat;

import static android.content.Context.CLIPBOARD_SERVICE;

public class SharePopup extends BottomPopupView {

    private String title, desc, link, imageLink;

    private @DrawableRes int drawableId;

    public SharePopup(@NonNull Context context) {
        super(context);
    }

    public SharePopup(@NonNull Context context, String title, String description, String link) {
        this(context, title, description, link, 0);
    }

    public SharePopup(@NonNull Context context, String title, String description, String link, @DrawableRes int drawableId) {
        super(context);
        this.desc = description;
        this.link = link;
        this.title = title;
        this.drawableId = drawableId;
    }

    @Override
    protected int getImplLayoutId() {

        return R.layout.popup_share;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        findViewById(R.id.tv_wechat).setOnClickListener(v -> share2WX());
        findViewById(R.id.tv_moments).setOnClickListener(v -> share2Friend());
        findViewById(R.id.button_cancel).setOnClickListener(v -> dismiss());

        findViewById(R.id.tv_link).setOnClickListener(v -> {
            dismiss();

            ClipboardManager cm = (ClipboardManager) getContext().getSystemService(CLIPBOARD_SERVICE);
            cm.setPrimaryClip(ClipData.newPlainText("Simple text", link));
            ToastUtils.makeText("复制成功");
        });

        findViewById(R.id.tv_download).setOnClickListener(v -> {


        });
    }

    // 分享至微信
    private void share2WX() {
        if (!MetaEraApplication.sWxApi.isWXAppInstalled()) {
            ToastUtils.makeText("您还未安装微信客户端");

        } else {
            new Thread(() -> share2WX(SendMessageToWX.Req.WXSceneSession)).start();
        }
    }


    // 分享至朋友圈
    private void share2Friend() {
        if (!MetaEraApplication.sWxApi.isWXAppInstalled()) {
            ToastUtils.makeText("您还未安装微信客户端");

        } else {
            new Thread(() -> share2WX(SendMessageToWX.Req.WXSceneTimeline)).start();
        }

        dismiss();
    }

    // 分享到微信
    private void share2WX(int scene) {
        try {
            // title, desc, link, image

            WXWebpageObject wxWebpageObject = new WXWebpageObject();
            wxWebpageObject.webpageUrl = link; // 链接
            WXMediaMessage wxMediaMessage = new WXMediaMessage(wxWebpageObject);
            wxMediaMessage.title = title; // 标题
            wxMediaMessage.description = desc; // 描述

            // 图片

            if (drawableId != 0){
                wxMediaMessage.setThumbImage(BitmapFactory.decodeResource(getResources(), drawableId));

            } else if (imageLink != null){
                Bitmap thumb = BitmapFactory.decodeStream(new URL("mShareModel.imageUrl").openStream());
                Bitmap thumbBmp = Bitmap.createScaledBitmap(thumb, 120, 150, true);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                thumbBmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                wxMediaMessage.thumbData = stream.toByteArray(); // 图片
                thumb.recycle();
            }

            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.scene = scene;
            req.message = wxMediaMessage;
            req.transaction = String.valueOf(System.currentTimeMillis());
            MetaEraApplication.sWxApi.sendReq(req);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            dismiss();
        }
    }

}
